package com.fyales.android.api.callback;


import com.fyales.android.api.response.HttpResponse;
import com.fyales.android.util.DevUtil;

import retrofit.Callback;
import retrofit.Response;
import retrofit.Retrofit;

/**
 * 网络请求回调类
 * @author fyales
 * @since 15/12/4.
 */
public abstract class HttpRequestCallBack<T> implements Callback<HttpResponse<T>> {

    /**
     * 网络请求成功且返回datas
     */
    public abstract void onSuccess(T ret);

    /**
     * 网络请求成功，但请求结果失败 ，即返回error_code及error_msg
     */
    public abstract void onError(String errorCode, String errorMsg);

    /**
     * 网络请求错误
     */
    public abstract void onFailure(String errorMsg);


    @Override
    public void onResponse(Response<HttpResponse<T>> response, Retrofit retrofit) {
        if (response.isSuccess()){
            HttpResponse ret = response.body();
            DevUtil.v("Retrofit", "Return value is " + ret.toString());
            if (ret.getResult() != null){
                onSuccess(response.body().getResult());
            }else{
                onError(ret.getError_code(),ret.getReason());
            }
        }
    }

    @Override
    public void onFailure(Throwable t) {
        DevUtil.v("Retrofit", "Error!The message is " + t.toString());
        onFailure(t.getMessage());
    }
}
